package com.zznj.xzjc.activity.index.zhishiku;

import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.KnowLedgeBasicAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.KnowledgeAndWorkBasic;
import com.zznj.xzjc.bean.index.KnowledgeAndWorkResult;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityZhiShiKuDetailJdjg extends BaseActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.info)
    TextView info;

    Unbinder unbinder;

    //基本信息
    @BindView(R.id.basicView)
    RecyclerView basicView;

    //内容
    @BindView(R.id.content)
    WebView webView;

    //基本信息
    @BindView(R.id.zjsjyqLinear)
    View zjsjyqLinear;
    @BindView(R.id.righImage)
    ImageView righImage;
    @BindView(R.id.rightInfo)
    TextView rightInfo;

    private LoginBean user;
    private String id;
    @Override
    public void initView() {
        id = getIntent().getExtras().getString("id");
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        setTitle("鉴定机构详情");
        initList();
        isCollect();
        initWebView();
    }

    private List<KnowledgeAndWorkBasic> mList;
    private KnowLedgeBasicAdapter mAdapter;
    private void initList() {
        mList = new ArrayList<>();
        ((SimpleItemAnimator) basicView.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        basicView.setLayoutManager(linearLayoutManager);
        mAdapter = new KnowLedgeBasicAdapter(R.layout.gyss_item_knowledge_basic, mList);
        basicView.setAdapter(mAdapter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_zhishiku_detail_jdjg_layout;
    }
    private void initWebView() {
        WebSettings wvSetting = webView.getSettings();
        wvSetting.setJavaScriptEnabled(true);
        wvSetting.setAllowFileAccess(true);
        wvSetting.setBuiltInZoomControls(false);
        wvSetting.setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/h5.html");
    }
    private KnowledgeAndWorkResult resultObj;
    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<KnowledgeAndWorkResult>> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                resultObj = baseBean.getResult();
                title.setText(baseBean.getResult().getTitle());
                info.setText("信息来源：" + StringUtils.toStringForWu(baseBean.getResult().getSourceName()));

                List<KnowledgeAndWorkBasic> basicList = new Gson().fromJson(resultObj.getBasicInfo(), new TypeToken<List<KnowledgeAndWorkBasic>>() {
                }.getType());
                mAdapter.setNewData(basicList);


                webView.loadUrl("javascript:insertData('"+baseBean.getResult().getContent().replace("src=\"","src=\"http://218.28.192.34:9017/pwlp-hubei")+"')");
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("id", id)
                .build();
        HttpMethods.getInstance().knowledgeGet(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private boolean isShouCang = false;
    private void isCollect() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                String data = baseBean.getMessage();
                if ("false".equals(data)) {
                    isShouCang = false;
                    righImage.setImageResource(R.mipmap.gyss_sc_wsc_white_img);
                    rightInfo.setText("收藏");
                } else {
                    isShouCang = true;
                    righImage.setImageResource(R.mipmap.gyss_sc_ysc_white_img);
                    rightInfo.setText("已收藏");
                }
                zjsjyqLinear.setVisibility(View.VISIBLE);
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("infoId", id)
                .add("userId", user.getUserId())
                .build();
        HttpMethods.getInstance().isCollection(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    @OnClick({R.id.zjsjyqLinear})
    public void onViewClick(View v){
        if(v.getId() == R.id.zjsjyqLinear){
            if(isShouCang){
                canCelShouCang();
            }else{
                shouCang();
            }
        }
    }

    private void shouCang(){
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                isShouCang = true;
                righImage.setImageResource(R.mipmap.gyss_sc_ysc_white_img);
                rightInfo.setText("已收藏");
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("infoId", id)
                .add("userId", user.getUserId())
                .add("title", title.getText().toString())
                .add("collectType", "knowledgeLib")
                .add("infoSign", "CheckupOrg")
                .build();
        HttpMethods.getInstance().addCollect(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void canCelShouCang(){
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                isShouCang = false;
                righImage.setImageResource(R.mipmap.gyss_sc_wsc_white_img);
                rightInfo.setText("收藏");
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("infoId", id)
                .add("userId", user.getUserId())
                .build();
        HttpMethods.getInstance().cancelCollect(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }
}
